//
//  DDInputPickerView.m
//  DDPickerKeyboardView
//
//  Created by zhipeng on 16/11/9.
//  Copyright © 2016年 lqd. All rights reserved.
//

#import "DDInputPickerView.h"
@interface DDInputPickerView () <UIPickerViewDelegate , UIPickerViewDataSource>

@property (strong , nonatomic) NSMutableArray * oneDataArray;

@property (strong , nonatomic) NSMutableArray * twoDataArray;

@property (strong , nonatomic) NSMutableArray * threeDataArray;

@property (assign , nonatomic) NSInteger oneComponent;

@property (assign , nonatomic) NSInteger twoComponent;

@property (assign , nonatomic) NSInteger threeComponent;

@end

@implementation DDInputPickerView
- (void)sendConfirm
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(returnSelecteOneString:TwoString:ThreeString:)]) {
        [self.delegate returnSelecteOneString:self.oneDataArray[self.oneComponent]
                                    TwoString:self.twoDataArray[self.twoComponent]
                                  ThreeString:self.threeDataArray[self.threeComponent]
         ];
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(returnSelecteOneComponent:TwoComponent:ThreeComponent:)]) {
        [self.delegate returnSelecteOneComponent:self.oneComponent
                                    TwoComponent:self.twoComponent
                                  ThreeComponent:self.threeComponent
         ];
    }
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.pickerView     = [[UIPickerView alloc] initWithFrame:frame];
        self.pickerView.center = CGPointMake([UIScreen mainScreen].bounds.size.width / 2.0, frame.size.height /2.5);
        self.oneComponent   = -1;
        self.twoComponent   = -1;
        self.threeComponent = -1;
        self.pickerView.delegate   = self;
        self.pickerView.dataSource = self;
        [self addSubview:self.pickerView];
    }
    return self;
}
#pragma mark ----- 懒加载 -----
- (NSMutableArray *)oneDataArray
{
    if (_oneDataArray == nil) {
        _oneDataArray = [NSMutableArray array];
    }
    return _oneDataArray;
}
- (NSMutableArray *)twoDataArray
{
    if (_twoDataArray == nil) {
        _twoDataArray = [NSMutableArray array];
    }
    return _twoDataArray;
}
- (NSMutableArray *)threeDataArray
{
    if (_threeDataArray == nil) {
        _threeDataArray = [NSMutableArray array];
    }
    return _threeDataArray;
}
- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
#pragma mark ----- 重写set方法 -----
- (void)setDataDict:(NSDictionary *)dataDict
{
    if (_dataDict != dataDict) {
        _dataDict = dataDict;
        /**
         *  设置展示数据
         */
        self.oneDataArray = _dataDict.allKeys.mutableCopy;
        if ([_dataDict[self.oneDataArray.lastObject] isKindOfClass:[NSDictionary class]])
        {
            NSDictionary * dict = _dataDict[self.oneDataArray.firstObject];
            self.twoDataArray = dict.allKeys.mutableCopy;
            if (dict[self.twoDataArray.firstObject] != nil && [dict[self.twoDataArray.firstObject] isKindOfClass:[NSArray class]]) {
                self.threeDataArray = dict[self.twoDataArray.firstObject];
            }else if (dict[self.twoDataArray.firstObject] != nil && [dict[self.twoDataArray.firstObject] isKindOfClass:[NSDictionary class]]){
                NSDictionary * dictionary = dict[self.twoDataArray.firstObject];
                self.threeDataArray = dictionary.allKeys.mutableCopy;
            }
        }else if ([_dataDict[self.oneDataArray.lastObject] isKindOfClass:[NSArray class]]){
            self.twoDataArray = _dataDict[self.oneDataArray.firstObject];
        }
        
        if (self.oneDataArray.count > 0) {
            [self.pickerView reloadAllComponents];
        }
    }
}
- (void)setDataArray:(NSMutableArray *)dataArray
{
    if (_dataArray != dataArray) {
        _dataArray = dataArray;
        /**
         *  设置展示数据
         */
        int i = 0;
        for (id obj in dataArray) {
            if ([obj isKindOfClass:[NSMutableArray class]]) {
                switch (i) {
                    case 0:
                        self.oneDataArray = obj;
                        break;
                    case 1:
                        self.twoDataArray = obj;
                        break;
                    case 2:
                        self.threeDataArray = obj;
                        break;
                    default:
                        break;
                }
            }else{
                self.oneDataArray = dataArray;
                
                if (self.oneDataArray.count > 0) {
                    [self.pickerView reloadAllComponents];
                }
                return;
            }
            i++;
        }
        if (self.oneDataArray.count > 0) {
            [self.pickerView reloadAllComponents];
        }
    }
}
- (void)setComponent:(NSUInteger)component
{
    if (_component != component) {
        _component = component;
        if (component == 1) {
            self.oneComponent = 0;
            return;
        }
        if (component == 2) {
            self.oneComponent = self.twoComponent = 0;
            return;
        }
        self.oneComponent = self.twoComponent = self.threeComponent = 0;
    }
}
#pragma mark ----- pickerViewDataSource -----
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{

    return self.component;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        return self.oneDataArray.count;
    }
    if (component == 1) {
        return self.twoDataArray.count;
    }
    return self.threeDataArray.count;
}
#pragma mark ----- pickerViewDelegate -----
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) {
        self.oneComponent   = row;
    }else if (component == 1) {
        self.twoComponent   = row;
    }else{
        self.threeComponent = row;
    }
    /**
     *  如果是动态排序,动态更新每一列显示的值。
     */
    if (self.isDynamic) {
        if (component == 0) {
            NSDictionary * dict = [self.dataDict valueForKey:self.oneDataArray[row]];
            self.twoDataArray   = [dict allKeys].mutableCopy;
            if (self.twoComponent > self.twoDataArray.count - 1) {
                self.twoComponent = self.twoDataArray.count - 1;
            }
            self.threeDataArray = [dict valueForKey:self.twoDataArray[self.twoComponent]];
            if (self.threeComponent > self.threeDataArray.count - 1) {
                self.threeComponent = self.threeDataArray.count - 1;
            }
            [self.pickerView reloadComponent:1];
            [self.pickerView reloadComponent:2];
        }else if (component == 1) {
            NSDictionary * dict = [self.dataDict valueForKey:self.oneDataArray[self.oneComponent]];
            if (![dict[self.twoDataArray[self.twoComponent]] isKindOfClass:[NSArray class]]) {
                return;
            }
            NSArray * array    = dict[self.twoDataArray[self.twoComponent]];
            self.threeDataArray = array.mutableCopy;
            if (self.threeComponent > self.threeDataArray.count - 1) {
                self.threeComponent = self.threeDataArray.count - 1;
            }
            [self.pickerView reloadComponent:2];
        }
    }
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0) {
        return self.oneDataArray[row];
    }else if (component == 1){
        return self.twoDataArray[row];
    }else{
        return self.threeDataArray[row];
    }
}

-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    
    CGFloat componentWidth = 0.0;
    if (component == 0)
        
        componentWidth = 100.0; // 第一个组键的宽度
    
    else if (component == 1)
        
        componentWidth = 100.0; // 第2个组键的宽度
    
    else if(component ==2)
        componentWidth = 175;
    
        return componentWidth;
}


@end
